import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class MoveGesture {
	private Robot r;
    public static final int xTopLeftCorner = 1575; // 614
    public static final int yTopLeftCorner = 388; // 122
    public static final int totalSize = 600;
    public static final int delta = 30;
    
	
	public static void main(String[] args) throws AWTException {
        final int xCorner = xTopLeftCorner + 29;
        final int yCorner = yTopLeftCorner + 564;

        new Robot().mouseMove(xCorner , yCorner );
    }
	
	public MoveGesture(Robot robot) {
		r = robot;
	}
	
	public void takeNextPiece() {
//	    final int xCorner = 614;
//	    final int yCorner = 900 - 707;
		final int xCorner = xTopLeftCorner;
		final int yCorner = yTopLeftCorner + 71;
		BufferedImage bufferedImage = r.createScreenCapture(new Rectangle(xCorner, yCorner, totalSize, 1));
		int delta = 2;
		int i = 0;
		while (i < totalSize / delta) {
			if (Identificator.isColored(bufferedImage.getRGB(i * delta, 0))) {
				click(xCorner + i * delta, yCorner);
				putToIdentificationPlace();
				break;
			}
			i++;
		}
	}
	
	public void putToIdentificationPlace() {
//      final int xDest = 690;
//      final int yDest = 900 - 550;
        final int xDest = xTopLeftCorner + 76;
        final int yDest = yTopLeftCorner + 228;
        click(xDest, yDest);
	}
	
	public void select(int x, int y) {
//		final int xCorner = 660;
//		final int yCorner = 900 - 592;
	    final int xCorner = xTopLeftCorner + 46;
	    final int yCorner = yTopLeftCorner + 186;
		click(xCorner + x * delta, yCorner + y * delta);
	}
	
	public void rotate(int times) {
		try {
			for (int i = 0; i < times; i++) {
				Thread.sleep(Main.refreshRate);
				r.keyPress(KeyEvent.VK_SPACE);
				Thread.sleep(Main.refreshRate);
				r.keyRelease(KeyEvent.VK_SPACE);
			}
		} catch (InterruptedException e) {}
	}
	
	public void putAt(Piece p, int x, int y) {
//      final int xCorner = 643;
//      final int yCorner = 900 - 603;
	    final int xCorner = xTopLeftCorner + 29;
	    final int yCorner = yTopLeftCorner + 175;
		int xDeltaSelect = (p.nColumns * delta) / 2;
		int yDeltaSelect = (p.nRows * delta) / 2;
		click(xCorner + x * delta + xDeltaSelect, yCorner + y * delta + yDeltaSelect);
	}
	
	private void click(int xDest, int yDest) {
		try {
			r.mouseMove(xDest, yDest);
			Thread.sleep(Main.refreshRate);
			r.mousePress(InputEvent.BUTTON1_MASK);
			Thread.sleep(Main.refreshRate);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
		} catch (InterruptedException e) {}
	}
	
	public void destroyPiece() {
		try {
//          final int xCorner = 623;
//          final int yCorner = 900 - 759;
		    final int xCorner = xTopLeftCorner + 9;
		    final int yCorner = yTopLeftCorner + 19;
			r.mouseMove(xCorner, yCorner);
			for (int i = 0; i < 5; i++) {
				Thread.sleep(Main.refreshRate);
				Toolkit.getDefaultToolkit().beep();
				r.mousePress(InputEvent.BUTTON1_MASK);
				Thread.sleep(Main.refreshRate);
				r.mouseRelease(InputEvent.BUTTON1_MASK);
			}
		} catch (InterruptedException e) {}
	}
	
	public void moveOutForSceenShot() {
//      final int xCorner = 643;
//      final int yCorner = 900 - 214;
        final int xCorner = xTopLeftCorner + 29;
        final int yCorner = yTopLeftCorner + 564;
		r.mouseMove(xCorner, yCorner);
	}
}
